package com.ete.app.ui.main

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.ete.app.BR
import com.ete.app.R
import com.ete.app.constant.EteConstant
import com.ete.app.databinding.ActivityMainBinding
import com.ete.service.entry.hardware.ConnectType
import com.ete.service.entry.hardware.SlaveEntry
import com.ete.service.manager.ManagerConstant
import com.ete.service.manager.hardware.HardwareManager
import com.ete.service.manager.location.LocationManager
import com.ete.service.manager.log.LogManager
import com.ete.service.manager.version.VersionManager
import com.ete.service.service.location.LocationUtil
import com.ete.service.util.DeviceUtils
import com.ete.service.util.unique.UniqueUtils
import com.jkkc.lib.base.core.BaseActivity
import com.jkkc.lib.base.extend.BOOLEAN_FALSE
import com.jkkc.lib.base.extend.BOOLEAN_TRUE
import com.jkkc.lib.base.extend.TIME_TWO_SECOND
import com.jkkc.lib.base.util.logcat.LogcatManager
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * Author:LiXian
 * <p>
 * Desc:首页
 * Version:1.0
 * date 2024/1/8 9:54
 */
@Route(path = EteConstant.ROUTER_HOME)
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.activity_main

    override fun initVariableId(): Int = BR.mainVm

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val filePath = EteConstant.CASH_LOG + "/中长跑.log"
        LogcatManager.startRecordLogcat(filePath) {}
    }

    override fun onResume() {
        super.onResume()
        addSlaveDevice()
    }

    private fun addSlaveDevice() {
        val mEntries: MutableList<SlaveEntry> = mutableListOf()
        mEntries.add(SlaveEntry(
                slaveId = UniqueUtils.getUniqueFlag(),
                slaveName = "跳绳",
                firmwareVersion = "1.2.1",
                hardwareVersion = "v5.3",
                connectType = ConnectType.CONNECT_24G
        ))
        mEntries.add(SlaveEntry(
                slaveId = UniqueUtils.getUniqueFlag(),
                slaveName = "仰卧起坐",
                firmwareVersion = "1.3.1",
                hardwareVersion = "v5.4",
                connectType = ConnectType.CONNECT_BLE
        ))
        mEntries.add(SlaveEntry(
                slaveId = UniqueUtils.getUniqueFlag(),
                slaveName = "心率网关",
                firmwareVersion = "1.3",
                hardwareVersion = "v5",
                connectType = ConnectType.CONNECT_58G
        ))
        HardwareManager.addSalveDeviceList(mEntries)
    }

    fun onClick(v: View) {
        when (v.id) {
            R.id.act_main_btn_1 -> uploadAppLogFile(false)
            R.id.act_main_btn_2 -> uploadHardwareInfo()
            R.id.act_main_btn_3 -> appUpgradeVersion()
            R.id.act_main_btn_4 -> cancelLogUpload()
            R.id.act_main_btn_5 -> uploadAppLogFile(true)
            R.id.act_main_btn_6 -> uploadSystemLog()
            R.id.act_main_btn_7 -> startLocation()
            R.id.act_main_btn_8 -> navigationOpenGps()
            R.id.act_main_btn_9 -> getLocationResult()

        }
    }

    private fun uploadSystemLog() {
        LogManager.uploadSystemLog()
    }

    private fun cancelLogUpload() {
        LogManager.cancelLogTask()
    }

    private fun appUpgradeVersion() {
        VersionManager.manualCheckNewVersion()
    }

    private fun uploadAppLogFile(isChooseDir: Boolean) {
        if (isChooseDir) {
            LogManager.chooseDirUploadAppLog()
        } else {
            LogManager.uploadAppLogFile()
        }
    }

    private fun uploadHardwareInfo() {
        HardwareManager.startUploadHardwareInfo()
    }

    private fun startLocation() {
        LocationManager.checkedPermission(mCallback = {
            if (it == ManagerConstant.CODE_SUCCESS) {
                val code = LocationManager.startLocation()
                ToastUtils.showShort(LocationManager.getErrorByCode(code))
            } else {
                val mError = LocationManager.getErrorByCode(it)
                ToastUtils.showShort(mError)
            }
            viewModel.initLocationMode()
        })
    }

    private fun navigationOpenGps() {
        LocationManager.navigationOpenGps()
    }

    private fun getLocationResult() {
        val result = LocationManager.getLocationResult()
        val content = "经度：${result.longitude},纬度:${result.latitude}"
        ToastUtils.showShort(content)
    }

    override fun onDestroy() {
        super.onDestroy()
        HardwareManager.stopUploadHardwareInfo()
        LocationManager.destroyLocation()
    }
}